package com.vivien.example.springbootdemo.controller;

import com.vivien.example.springbootdemo.pojo.Goods;
import com.vivien.example.springbootdemo.service.GoodsService;
import com.vivien.example.springbootdemo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService service;

    @GetMapping("goods")
    public Result getAll(){
        Result r = new Result(200,"成功",service.getAll());
        return r;
    }

    //删除、查询详情
    @GetMapping("goods/{id}")
    public Result getOne(@PathVariable("id") int id){

        try{
            int a=0;
            int b=5;
            int x=b/a;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return new Result(5001,"失败",null);
        }
        return new Result(200,"成功",service.getOne(id));
    }

    //新增、修改
    @PostMapping("goods")
    public Result addGoods(@RequestBody Goods goods){
        return new Result(200,"成功",service.addOne(goods));
    }

    //http://localhost:8080/getuser/1?name=aa&age=18    query
    @GetMapping("getuser/{id}")
    public void getUser(@RequestParam("name") String name,
                        @RequestParam(name = "age") int age,
                        @PathVariable(name = "id") int id){
        System.out.println(name);
        System.out.println(age);
        System.out.println(id);
    }

    public void getCookie(@CookieValue("mycokie") String cookie,
                          @RequestHeader("headkey") String header){

    }
}
